﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ServerStart
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.Elapsed += Timer_Elapsed;
            new Thread(new ThreadStart(Init)).Start();
        }

        private MyService service;
        private System.Timers.Timer timer = null;

        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            try
            {
                service = new MyService();
                service.Main2();
                ShowStatus();
                timer.Enabled = true;
                timer.Start();
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteLine("初始化失败");
                NewLife.Log.XTrace.WriteException(ex);
            }
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {

            try
            {
                ShowStatus();
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 显示状态
        /// </summary>
        private void ShowStatus()
        {
            try
            {
                if (service == null)
                {
                    NewLife.Log.XTrace.WriteLine("服务未初始化");
                    return;
                }
                int state = service.GetStatus();
                //btnInsert.Enabled = false;
                this.Dispatcher.Invoke(new Action(() =>
                {
                    btnInstall.IsEnabled = true;
                    if (state == 0)
                    {
                        btnStart.IsEnabled = false;
                        btnStop.IsEnabled = false;
                        lbStatus.Content = "服务未安装";
                        lbStatus.Foreground = Brushes.DarkGray;
                        btnInstall.Content = "安装";
                    }
                    else
                    {
                        btnInstall.Content = "卸载";
                        switch (state)
                        {
                            case 1:
                                lbStatus.Content = "服务正在运行...";
                                lbStatus.Foreground = Brushes.Green;
                                btnStop.IsEnabled = true;
                                btnStart.IsEnabled = false;
                                break;
                            case 2:
                                lbStatus.Content = "服务未运行";
                                lbStatus.Foreground = Brushes.Red;
                                btnStart.IsEnabled = true;
                                btnStop.IsEnabled = false;
                                break;
                            default:
                                break;
                        }
                    }
                }));
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (service == null)
                    return;
                int state = service.GetStatus();
                if (state == 2)
                {
                    service.Start();
                }
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
                MessageBox.Show("启动服务失败，" + ex.Message);
            }
            ShowStatus();
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (service == null)
                    return;
                int state = service.GetStatus();
                if (state == 1)
                {
                    service.Stop();
                }
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
                MessageBox.Show("停止服务失败，" + ex.Message);
            }
            ShowStatus();
        }

        private void btnInstall_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                if (service == null)
                    return;
                int state = service.GetStatus();
                if (state == 0)
                {
                    service.Install();
                }
                else
                {
                    if (state == 1)
                    {
                        service.Stop();
                        Thread.Sleep(500);
                    }
                    service.UnInstall();
                }
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
                MessageBox.Show(btnInstall.Content + "服务失败，" + ex.Message);
            }
            ShowStatus();
        }
    }
}
